<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
          <div class="header">
          <span>共 {{total}} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="openDrawer('add')"
            >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="stem"
        label="标题"
        width="400">
      </el-table-column>
      <el-table-column
        prop="creator"
        label="作者">
      </el-table-column>
       <el-table-column
        prop="likeCount"
        label="点赞">
      </el-table-column>
       <el-table-column
        prop="views"
        label="浏览数">
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="更新时间">
      </el-table-column>
      <el-table-column
        width="200"
        label="操作">
          <!-- 这里就是插槽的位置 -->
          <!-- 作用域插槽: 获取组件内传递过来的数据 -->
          <!-- <slot :item="" :row=""></slot> => obj: { item, row } -->
        <template v-slot="{row}">
       <div class="actions">
        <i
          class="el-icon-view" @click="openDrawer('preview',row.id)"
        ></i>
       <i class="el-icon-edit-outline" @click="openDrawer('edit', row.id)"></i>
        <i class="el-icon-delete" @click="del(row.id)"></i>
      </div>
        </template>
      </el-table-column>
    </el-table>
      <!--
        background: 带背景色
        layout="prev, pager, next" 分页器的布局
        total: 总数量, 会自动计算一共有多少页, 默认情况下每页显示十条
        pageSize: 设置每页显示多少条数据
       -->
       <el-pagination
        @current-change="hChange"
        background
        layout="prev, pager, next"
        :pageSize="pageSize"
        :total="total">
      </el-pagination>
    </el-card>
      <!-- 抽屉组件 -->
    <el-drawer
  :visible.sync="isShowDrawer"
  :title="drawerTitle"
  direction="rtl"
  size="50%"
>
  <!--
      from 来自于...
      form 表单
      el-form 身上的 model 属性
      el-form 身上的 rules 属性
      el-form-item 身上的 prop 属性
      表单元素的双向绑定
     -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- $refs.form.validateField('content') 对单独的表单字段进行校验, 如果不符合条件会提示在元素下方 -->
          <quillEditor @blur="$refs.form.validateField('content')" v-model="form.content"></quillEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">确认</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { getArticleList } from '@/api/article'
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      list: [],
      total: 0,
      current: 1,
      pageSize: 10,
      isShowDrawer: false,
      drawerType: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          // 一个对象就是一条校验规则
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ],
        content: [
          // 一个对象就是一条校验规则
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    // 页面加载就要调用获取数据的方法
    this.loadData()
  },
  methods: {
    del (id) {
      const index = this.list.findIndex(item => item.id === id)
      // 根据索引删除数据
      this.list.splice(index, 1)
      this.$message.success('删除成功')
    },
    hChange (current) {
      // console.log(current)
      // 修改当前页
      this.current = current
      // 重新加载数据 => 覆盖
      this.loadData()
    },
    async loadData () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    openDrawer (type, id) {
      console.log(type, id)
      this.isShowDrawer = true
      this.drawerType = type
    }
  },
  computed: {
    drawerTitle () {
      // if (this.drawerType === 'add') {
      //   return '添加面经'
      // }
      // else if (this.drawerType === 'preview') {
      //   return '面经预览'
      // }
      // else {
      //   return '修改面经'
      // }
      // 数据字典的思路
      const obj = {
        add: '添加面经',
        preview: '面经预览',
        edit: '修改面经'
      }
      return obj[this.drawerType]
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
